package com.yugao.fintech.libra.shortlink.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.framework.core.annotations.FieldUniqueCheck;
import com.yugao.fintech.framework.core.enums.OperationTypeEnum;
import com.yugao.fintech.libra.shortlink.mapper.SlLinkGroupMapper;
import com.yugao.fintech.libra.shortlink.model.entity.LinkGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

/**
 * 短链组仓储层
 */
@Slf4j
@Repository
public class SlLinkGroupManager extends ServiceImpl<SlLinkGroupMapper, LinkGroup> implements IService<LinkGroup> {

    @FieldUniqueCheck(type = OperationTypeEnum.UPDATE)
    public boolean updateByIdAndAccount(LinkGroup entity) {
        LambdaUpdateWrapper<LinkGroup> wrapper = LinkGroup.luw()
                .eq(LinkGroup::getUserId, entity.getUserId()).eq(LinkGroup::getId, entity.getId());
        return this.update(entity, wrapper);
    }

    public LinkGroup getByIdAndUserId(Long groupId, Long userId) {
        LambdaQueryWrapper<LinkGroup> wrapper = LinkGroup.lqw()
                .eq(LinkGroup::getUserId, userId).eq(LinkGroup::getId, groupId);
        return this.getOne(wrapper);
    }

    @FieldUniqueCheck(type = OperationTypeEnum.SAVE)
    public boolean saveGroup(LinkGroup entity) {
        return this.save(entity);
    }
}
